package com.work.work.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work.work.common.R;
import com.work.work.dto.ProjectResourceDetailDTO;
import com.work.work.entity.Resource;
import com.work.work.service.ResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@Tag(name = "资源管理")
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @Operation(summary = "创建资源")
    @PostMapping("/create")
    public R<Resource> create(@RequestBody Resource resource) {
        if (resource.getResourceName() == null || resource.getResourceName().trim().isEmpty()) {
            return R.fail("资源名称不能为空");
        }
        if (resource.getResourceType() == null || resource.getResourceType().trim().isEmpty()) {
            return R.fail("资源类型不能为空");
        }
        if (resource.getQuantity() == null) {
            return R.fail("资源数量不能为空");
        }

        return resourceService.save(resource) ? R.ok(resource, "资源创建成功") : R.fail("创建资源失败");
    }

@Operation(summary = "更新资源")
@PutMapping("/update")
public R<Boolean> update(@RequestBody Resource resource) {
    // 验证参数
    if (resource.getId() == null) {
        return R.fail("资源ID不能为空");
    }
    if (resource.getResourceName() == null || resource.getResourceName().trim().isEmpty()) {
        return R.fail("资源名称不能为空");
    }
    if (resource.getResourceType() == null || resource.getResourceType().trim().isEmpty()) {
        return R.fail("资源类型不能为空");
    }
    if (resource.getQuantity() == null) {
        return R.fail("资源数量不能为空");
    }
    if (resource.getUnit() == null || resource.getUnit().trim().isEmpty()) {
        return R.fail("资源单位不能为空");
    }

    try {
        // 检查资源是否存在
        Resource existingResource = resourceService.getById(resource.getId());
        if (existingResource == null) {
            return R.fail("资源不存在");
        }

        // 更新资源
        boolean success = resourceService.updateById(resource);
        return success ?
            R.ok(true, "更新成功") :
            R.fail("更新失败");
    } catch (Exception e) {
        log.error("更新资源失败", e);
        return R.fail("更新失败：" + e.getMessage());
    }
}

    @Operation(summary = "删除资源")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Integer id) {
        return resourceService.removeById(id) ? R.ok(true, "项目创建成功") : R.fail("删除资源失败");
    }

    @Operation(summary = "获取资源详情")
    @GetMapping("/{id}")
    public R<Resource> getById(@PathVariable Integer id) {
        Resource resource = resourceService.getById(id);
        return resource != null ? R.ok(resource, "项目创建成功") : R.fail("资源不存在");
    }

    @Operation(summary = "分页查询资源")
    @GetMapping("/page")
    public R<Page<Resource>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer projectId,
            @RequestParam(required = false) String resourceType) {

        LambdaQueryWrapper<Resource> queryWrapper = new LambdaQueryWrapper<>();

        // 如果指定了资源类型，添加类型过滤条件
        if (resourceType != null && !resourceType.trim().isEmpty()) {
            queryWrapper.eq(Resource::getResourceType, resourceType);
        }

        // 如果需要按项目ID查询，应该使用专门的方法
        if (projectId != null) {
            return R.ok(resourceService.getResourcesByProjectId(projectId, current, size), "查询成功");
        }

        // 默认排序
        queryWrapper.orderByAsc(Resource::getId);

        return R.ok(resourceService.page(new Page<>(current, size), queryWrapper), "查询成功");
    }
    @Operation(summary = "获取所有资源")
    @GetMapping("/list")
    public R<List<Resource>> list() {
        List<Resource> resources = resourceService.list();
        return R.ok(resources, "查询成功");
    }
    @Operation(summary = "获取项目资源使用详情")
    @GetMapping("/project/{projectId}")
    public R<ProjectResourceDetailDTO> getProjectResourceDetail(@PathVariable Integer projectId) {
        try {
            ProjectResourceDetailDTO detail = resourceService.getProjectResourceDetail(projectId);
            return R.ok(detail, "获取成功");
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}